package com.ebelter.myehc.adapters.historys;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.response.GetBC;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ebelter.btcomlib.bases.fragment.BaseFragment.N2;


public class H_BC_A extends BaseAdapter {

    private Context mContext;

    private List<GetBC.ResultDataBean.DataListBean> datas;

    public H_BC_A(Context mContext) {
        this.mContext = mContext;
        datas=new ArrayList<>();
    }


    public void addData(List<GetBC.ResultDataBean.DataListBean> data){
        datas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas!=null?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        VH vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.item_scale_his,null);
            vh=new VH();
            vh.topLine=convertView.findViewById(R.id.item_bc_top_h_line);
            vh.buttomLine=convertView.findViewById(R.id.item_bc_buttom_h_line);

            vh.item_s_time_tv=convertView.findViewById(R.id.item_s_time_tv);
            vh.item_s_date_tv=convertView.findViewById(R.id.item_s_date_tv);

            vh.item_s_weight_tv=convertView.findViewById(R.id.item_s_weight_tv);
            vh.item_s_fat_tv=convertView.findViewById(R.id.item_s_fat_tv);
            vh.item_s_muscle_tv=convertView.findViewById(R.id.item_s_muscle_tv);
            vh.item_s_bmr_tv=convertView.findViewById(R.id.item_s_bmr_tv);
            vh.item_s_water_tv=convertView.findViewById(R.id.item_s_water_tv);
            vh.item_s_infat_tv=convertView.findViewById(R.id.item_s_infat_tv);
            vh.item_s_bone_tv=convertView.findViewById(R.id.item_s_bone_tv);
            vh.item_s_protein_tv=convertView.findViewById(R.id.item_s_protein_tv);
            vh.item_s_bmi_tv=convertView.findViewById(R.id.item_s_bmi_tv);
            convertView.setTag(vh);
        }else {
            vh= (VH) convertView.getTag();
        }




        Date date = TimeUtils.parseFormatter1Time(datas.get(position).time);
        String shiFen = TimeUtils.formatTime4(date.getTime());
        //String yueRi = TimeUtils.formatTime11(date.getTime());

        String yueRi = TimeUtils.timeFatmat(datas.get(position).time,1);
        vh.item_s_time_tv.setText(shiFen);//时-分
        vh.item_s_date_tv.setText(yueRi);//月-日



        GetBC.ResultDataBean.DataListBean currentB=datas.get(position);



        vh.item_s_weight_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.weight),N2)+"");
        vh.item_s_fat_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.adiposerate),N2)+"");
        vh.item_s_muscle_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.muscle),N2)+"");
        vh.item_s_bmr_tv.setText(NumUtils.disIntZero((int) NumUtils.g0Bl1(currentB.basalMetabolism),N2)+"");
        vh.item_s_water_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.moisture),N2)+"");
        vh.item_s_infat_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.visceralfat),N2)+"");
        vh.item_s_bone_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.bone),N2)+"");
        vh.item_s_protein_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.protein),N2)+"");
        vh.item_s_bmi_tv.setText(NumUtils.disFlZero(NumUtils.g0Bl1(currentB.bmi),N2)+"");


        adapterButtomLineView(vh.buttomLine,position,yueRi);




        return convertView;
    }


    /**
     *
     * @param buttomLine 底部横线控件
     * @param position 当前位置position
     * @param currentYueRi 当前月-日字符串
     */
    private void adapterButtomLineView(View buttomLine, int position, String currentYueRi) {

        try {

            ViewUtils.displayView(buttomLine);
            String nextYueRi = TimeUtils.timeFatmat(datas.get(position+1).time,1);

            if (TextUtils.equals(currentYueRi,nextYueRi)){
                //当前的月日日期跟下一个的月日日期是一样的 底部横线gone
                ViewUtils.goneView(buttomLine);
            }



        }catch (Exception e){
            e.printStackTrace();
        }




    }


    static class VH{
        View topLine;
        View buttomLine;
        TextView item_s_time_tv;
        TextView item_s_date_tv;
        TextView item_s_weight_tv;
        TextView item_s_fat_tv;
        TextView item_s_muscle_tv;
        TextView item_s_bmr_tv;
        TextView item_s_water_tv;
        TextView item_s_infat_tv;
        TextView item_s_bone_tv;
        TextView item_s_protein_tv;
        TextView item_s_bmi_tv;
     }
}
